package com.hibernate.db;

import java.util.List;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.junit.Test;

import com.hibernate.domain.Person;
import com.hibernate.utils.HibernateUtils;

public class PersonDbTest extends HibernateUtils{

	@Test
	public void testSavePerson() {
		Session session = sessionFactory.openSession() ;
		Transaction transaction = session.beginTransaction() ;
		Person person = new Person() ;
		person.setName("啧啧啧") ;
		person.setDescription("1111") ;
		session.save(person) ;
		transaction.commit() ;
		session.close() ;
	}
	
	@Test
	public void testUpdatePerson() {
		Session session = sessionFactory.openSession() ;
		Transaction transaction = session.beginTransaction() ;
		Person person = (Person)session.get(Person.class, 3L) ;
		person.setDescription("2222") ;
		session.update(person) ;
		transaction.commit() ;
		session.close() ;
	}
	
	@Test
	public void testDeletePerson() {
		Session session = sessionFactory.openSession() ;
		Transaction transaction = session.beginTransaction() ;
		Person person = (Person)session.get(Person.class, 3L) ;
		session.delete(person) ;
		transaction.commit() ;
		session.close() ;
	}
	
	@Test
	public void testFindAllPerson() {
		Session session = sessionFactory.openSession() ;
		List<Person> persons = session.createQuery("from Person").list() ;
		for (Person person : persons) {
			System.out.println(person);
		}
		session.close() ;
	}
	
	
}
